package com.idc.quotation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.idc.quotation.entity.*;
import com.idc.quotation.mapper.QuotationSysVersionMapper;
import com.idc.quotation.mapper.QuotationSystemMapper;
import com.idc.quotation.mapper.QuotationSystemVerModelMapper;
import com.idc.quotation.mapper.SystemClassificationMapper;
import com.idc.quotation.service.QuotationInfoService;
import com.idc.quotation.vo.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class QuotationInfoServiceImpl implements QuotationInfoService {
    private static final Logger log = LoggerFactory.getLogger(QuotationInfoServiceImpl.class);
    private final SystemClassificationMapper systemClassificationMapper;
    private final QuotationSystemMapper quotationSystemMapper;
    private final QuotationSysVersionMapper quotationSysVersionMapper;
    private final QuotationSystemVerModelMapper quotationSystemVerModelMapper;
    //智能工作台大类id
    @Value("${intelligentWorkbench.classificationName}")
    private String intelligentWorkbenchName;
    public QuotationInfoServiceImpl(SystemClassificationMapper systemClassificationMapper,QuotationSystemMapper quotationSystemMapper,
                                    QuotationSysVersionMapper quotationSysVersionMapper,QuotationSystemVerModelMapper quotationSystemVerModelMapper){
        this.systemClassificationMapper = systemClassificationMapper;
        this.quotationSystemMapper = quotationSystemMapper;
        this.quotationSysVersionMapper = quotationSysVersionMapper;
        this.quotationSystemVerModelMapper = quotationSystemVerModelMapper;
    }

    @Override
    public List<SystemClassification> querySystemClassification(Integer id) {
        List<SystemClassification> systemClassification = null;
        if(Objects.isNull(id)){
            systemClassification = systemClassificationMapper.selectList(new QueryWrapper<>());
        }else{
            systemClassification = systemClassificationMapper.selectList(new QueryWrapper<SystemClassification>().eq("id",id));
        }

        return systemClassification;
    }

    @Override
    public List<QuotationSystem> querySystemByClassificationId(Integer classificationId) {
        return quotationSystemMapper.selectList(new QueryWrapper<QuotationSystem>().eq("system_classification_id",classificationId));
    }

    @Override
    public List<QuotationSystemVersion> queryVersionBySysId(Integer sysId) {
        return quotationSysVersionMapper.selectList(new QueryWrapper<QuotationSystemVersion>().eq("system_id",sysId));
    }

    @Override
    public List<SystemVerModel> queryVerModel(Integer versionId) {
        return quotationSystemVerModelMapper.selectList(new QueryWrapper<SystemVerModel>().eq("version_id",versionId));
    }

    @Override
    public CompQuotationInfoVo queryQuotationInfo() {
        CompQuotationInfoVo compQuotationInfoVo = new CompQuotationInfoVo();
        List<SystemClassification> systemClassificationList = systemClassificationMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(systemClassificationList)){
            List<SystemClassificationVo> systemClassificationVoList = new ArrayList<>();
            for(SystemClassification obj:systemClassificationList){
                SystemClassificationVo systemClassificationVo = new SystemClassificationVo();
                systemClassificationVo.setId(obj.getId());
                systemClassificationVo.setSystemClassificationName(obj.getSystemClassificationName());
                systemClassificationVoList.add(systemClassificationVo);

                List<QuotationSystem> systemList = quotationSystemMapper.selectList(new QueryWrapper<QuotationSystem>().eq("system_classification_id",obj.getId()));

                List<QuotationSystemVersionInfoVo> systemVersionInfoVoList = new ArrayList<>();
                for(QuotationSystem sys:systemList){
                    List<QuotationSystemVersion> versionList = quotationSysVersionMapper.selectList(new QueryWrapper<QuotationSystemVersion>().eq("system_id",sys.getId()));
                    for(QuotationSystemVersion ver:versionList){
                        QuotationSystemVersionInfoVo systemVersionInfoVo = new QuotationSystemVersionInfoVo();
                        String verName = sys.getSystemName()+" (" + ver.getVersion() +")";
                        systemVersionInfoVo.setSysVersionName(verName);
                        systemVersionInfoVoList.add(systemVersionInfoVo);
                        systemClassificationVo.setSystemVersionModelVoList(systemVersionInfoVoList);

                        List<SystemVerModel> verModelList = quotationSystemVerModelMapper.selectList(new QueryWrapper<SystemVerModel>().eq("version_id",ver.getId()));
                        List<QuotationSysVerModelVo>  sysVerModelVoList = new ArrayList<>();
                        for(SystemVerModel verModel:verModelList){
                            QuotationSysVerModelVo verModelVo = new QuotationSysVerModelVo();
                            verModelVo.setModelName(verModel.getModelName());
                            verModelVo.setModelUnit(verModel.getModelUnit());
                            verModelVo.setModelConfig(verModel.getModelConfig());
                            verModelVo.setModelConfigName(verModel.getModelConfig() == 1?"标配":"选配");
                            verModelVo.setModelPrice(verModel.getModelPrice());
                            verModelVo.setModelDesc(verModel.getModelDesc());
                            sysVerModelVoList.add(verModelVo);
                            systemVersionInfoVo.setSysVerModelVoList(sysVerModelVoList);
                        }

                    }
                }
            }

            //处理智能工作台
            if(CollectionUtil.isNotEmpty(systemClassificationVoList)){
                List<IntelligentWorkbenchVo> intelligentWorkbenchVoList = new ArrayList<>();
                List<SystemClassificationVo> intelligentWorkbench = systemClassificationVoList.stream().filter(obj->obj.getSystemClassificationName().equals(intelligentWorkbenchName)).collect(Collectors.toList());
                SystemClassificationVo systemClassificationVo = intelligentWorkbench.get(0);
                List<QuotationSystemVersionInfoVo> systemVersionModelVoList = systemClassificationVo.getSystemVersionModelVoList();
                for(QuotationSystemVersionInfoVo sysVer:systemVersionModelVoList){
                    List<QuotationSysVerModelVo>  sysVerModelVoList = sysVer.getSysVerModelVoList();
                    for(QuotationSysVerModelVo verModel:sysVerModelVoList){
                        IntelligentWorkbenchVo intelligentWorkbenchVo = new IntelligentWorkbenchVo();
                        intelligentWorkbenchVo.setClassificationName("【标配】" + systemClassificationVo.getSystemClassificationName());
                        intelligentWorkbenchVo.setVersion("标准版");
                        intelligentWorkbenchVo.setModelConfig("标配");
                        intelligentWorkbenchVo.setSystemName(verModel.getModelName());
                        intelligentWorkbenchVo.setUnitPrice(verModel.getModelPrice());
                        intelligentWorkbenchVo.setModelDesc(verModel.getModelDesc());
                        intelligentWorkbenchVo.setModelUnit(verModel.getModelUnit());
                        intelligentWorkbenchVoList.add(intelligentWorkbenchVo);
                    }
                }
                compQuotationInfoVo.setIntelligentWorkbenchVoList(intelligentWorkbenchVoList);
                List<SystemClassificationVo> systemClassificationVoFilterZntList = systemClassificationVoList.stream().filter(obj->!obj.getSystemClassificationName().equals(intelligentWorkbenchName)).collect(Collectors.toList());
                compQuotationInfoVo.setSystemClassificationVoList(systemClassificationVoFilterZntList);
            }

        }


        return compQuotationInfoVo;
    }

    @Override
    public CompQuotationInfoVo queryQuotationInfoV1() {
        CompQuotationInfoVo compQuotationInfoVo = new CompQuotationInfoVo();
        List<SystemClassification> systemClassificationList = systemClassificationMapper.selectList(new QueryWrapper<>());
        if(CollectionUtil.isNotEmpty(systemClassificationList)){
            List<SystemClassificationVo> systemClassificationVoList = new ArrayList<>();
            compQuotationInfoVo.setSystemClassificationVoList(systemClassificationVoList);
            for(SystemClassification obj:systemClassificationList){
                SystemClassificationVo systemClassificationVo = new SystemClassificationVo();
                systemClassificationVo.setId(obj.getId());
                systemClassificationVo.setSystemClassificationName(obj.getSystemClassificationName());
                systemClassificationVoList.add(systemClassificationVo);

                List<QuotationSystem> systemList = quotationSystemMapper.selectList(new QueryWrapper<QuotationSystem>().eq("system_classification_id",obj.getId()));
                List<QuotationSystemVo> systemInfoVoList = new ArrayList<>();
                systemClassificationVo.setSysList(systemInfoVoList);
                for(QuotationSystem sys:systemList){
                    //创建每类对象
                    QuotationSystemVo quotationSystemVo = new QuotationSystemVo();
                    systemInfoVoList.add(quotationSystemVo);
                    quotationSystemVo.setSystemName(sys.getSystemName());
                    quotationSystemVo.setId(sys.getId());

                    List<SysModelVo> sysModes = quotationSystemVerModelMapper.queryModelBySysId(sys.getId());
                    Set<Integer> modelConfigSet = sysModes.stream().map(SysModelVo::getModelConfig).collect(Collectors.toSet());
                    //log.info(">>>>>>>>>>>modelConfigSet==={}",JSON.toJSON(modelConfigSet));
                    for(Integer mc:modelConfigSet){

                        String mcName = mc ==1?"标配":"选配";
                        quotationSystemVo.setSystemName(sys.getSystemName() + "【" + mcName + "】");
                        List<SysModelVo> sysModeFileterByModelConfiglList = sysModes.stream().filter(o->o.getModelConfig().equals(mc)).collect(Collectors.toList());
                        //log.info(">>>>>>>>>>>sysModeFileterByModelConfiglList==={}",JSON.toJSON(sysModeFileterByModelConfiglList));

                        //获取版本
                        Set<String> versionNameSet = sysModeFileterByModelConfiglList.stream().map(SysModelVo::getVersionName).collect(Collectors.toSet());
                        //log.info(">>>>>>>>>>>versionNameSett==={}",JSON.toJSON(versionNameSet));

                        quotationSystemVo.setVersionNameList(new ArrayList<>(versionNameSet));

                        //过滤标配和选配
                        Map<String,List<QuotationSysVerModelVo>> map = new HashMap<>();
                        for(String verName:versionNameSet){
                            List<SysModelVo> modelFilterByVersionList = sysModeFileterByModelConfiglList.stream().filter(o->o.getVersionName().equals(verName)).collect(Collectors.toList());
                            List<QuotationSysVerModelVo> innerModelList = BeanUtil.copyToList(modelFilterByVersionList,QuotationSysVerModelVo.class);
                            map.put(verName,innerModelList);
                        }
                        quotationSystemVo.setModelList(map);

                    }
                }
            }

            //处理智能工作台
            if(CollectionUtil.isNotEmpty(systemClassificationVoList)){
                List<IntelligentWorkbenchVo> intelligentWorkbenchVoList = new ArrayList<>();
                List<SystemClassificationVo> intelligentWorkbench = systemClassificationVoList.stream().filter(obj->obj.getSystemClassificationName().equals(intelligentWorkbenchName)).collect(Collectors.toList());
                SystemClassificationVo systemClassificationVo = intelligentWorkbench.get(0);
                List<QuotationSystemVo> systemVersionModelVoList = systemClassificationVo.getSysList();
                for(QuotationSystemVo sys:systemVersionModelVoList){
                    Map<String, List<QuotationSysVerModelVo>> map = sys.getModelList();
                    for(Map.Entry<String, List<QuotationSysVerModelVo>> entry:map.entrySet()){
                        List<QuotationSysVerModelVo> models = entry.getValue();
                        for(QuotationSysVerModelVo m:models){
                            IntelligentWorkbenchVo intelligentWorkbenchVo = new IntelligentWorkbenchVo();
                            intelligentWorkbenchVo.setClassificationName("【标配】" + systemClassificationVo.getSystemClassificationName());
                            intelligentWorkbenchVo.setVersion("标准版");
                            intelligentWorkbenchVo.setModelConfig("标配");
                            intelligentWorkbenchVo.setSystemName(m.getModelName());
                            intelligentWorkbenchVo.setUnitPrice(m.getModelPrice());
                            intelligentWorkbenchVo.setModelDesc(m.getModelDesc());
                            intelligentWorkbenchVo.setModelUnit(m.getModelUnit());
                            intelligentWorkbenchVoList.add(intelligentWorkbenchVo);
                        }
                    }
                }
                compQuotationInfoVo.setIntelligentWorkbenchVoList(intelligentWorkbenchVoList);
                List<SystemClassificationVo> systemClassificationVoFilterZntList = systemClassificationVoList.stream().filter(obj->!obj.getSystemClassificationName().equals(intelligentWorkbenchName)).collect(Collectors.toList());
                compQuotationInfoVo.setSystemClassificationVoList(systemClassificationVoFilterZntList);
            }

        }


        return compQuotationInfoVo;
    }

    @Override
    public CompQuotationInfoVo queryQuotationInfoV2() {
        CompQuotationInfoVo compQuotationInfoVo = new CompQuotationInfoVo();
        //一级大类
        List<SystemClassificationVo> systemClassificationVoList = new ArrayList<>();
        compQuotationInfoVo.setSystemClassificationVoList(systemClassificationVoList);

        //获取所有数据
        List<ClassSysVerModel> classSysVerModelList = systemClassificationMapper.queryQuotationInfo();
        //按照大类进行分组
        Map<Integer,List<ClassSysVerModel>> infoByClaIdGrooupMap = classSysVerModelList.stream().collect(Collectors.groupingBy(ClassSysVerModel::getClassId));
        for(Map.Entry<Integer,List<ClassSysVerModel>> entry:infoByClaIdGrooupMap.entrySet()){
            //处理大分类
            SystemClassificationVo systemClassificationVo = new SystemClassificationVo();

            systemClassificationVo.setId(entry.getKey());
            List<ClassSysVerModel> classSysVerModels = entry.getValue();
            ClassSysVerModel classSysVerModel = classSysVerModels.get(0);
            systemClassificationVo.setSystemClassificationName(classSysVerModel.getClassName());
            systemClassificationVoList.add(systemClassificationVo);

            //获取大类下的系统,key为系统id
            Map<Integer,List<ClassSysVerModel>> sysMap = classSysVerModels.stream().collect(Collectors.groupingBy(ClassSysVerModel::getSysId));
            List<QuotationSystemVo> sysList = new ArrayList<>();
            systemClassificationVo.setSysList(sysList);
            for(Map.Entry<Integer,List<ClassSysVerModel>> sys:sysMap.entrySet()){
                List<ClassSysVerModel> oneSysList = sys.getValue();
                //获取系统名称
                Optional<String> sysName = oneSysList.stream().map(ClassSysVerModel::getSysName).distinct().findFirst();
                //获取标配还是选配
                Map<Integer,List<ClassSysVerModel>> configMap = oneSysList.stream().collect(Collectors.groupingBy(ClassSysVerModel::getModelConfig));
                for(Map.Entry<Integer,List<ClassSysVerModel>> sysConfig:configMap.entrySet()){
                    int modelConfigValue = sysConfig.getKey();
                    List<ClassSysVerModel> sysConfigList = sysConfig.getValue();
                    String modelConfigName = modelConfigValue == 1?"标配":"选配";
                    QuotationSystemVo quotationSystemVo = new QuotationSystemVo();
                    sysList.add(quotationSystemVo);
                    quotationSystemVo.setId(sys.getKey());
                    //系统分类是以系统名称+模块的配置组合形式
                    quotationSystemVo.setSystemName(sysName.orElse("无名系统") + "【" + modelConfigName + "】");
                    //获取该配置项下的所有版本
                    Set<String> versionNameSet = sysConfigList.stream().map(ClassSysVerModel::getVersion).collect(Collectors.toSet());
                    quotationSystemVo.setVersionNameList(new ArrayList<>(versionNameSet));
                    //以版本进行分组
                    Map<String,List<ClassSysVerModel>> modelListMap = sysConfigList.stream().collect(Collectors.groupingBy(ClassSysVerModel::getVersion));
                    Map<String,List<QuotationSysVerModelVo>> modelListMapNew = new HashMap<>();
                    for(Map.Entry<String,List<ClassSysVerModel>> entry1:modelListMap.entrySet()){
                        String key = entry1.getKey();
                        List<ClassSysVerModel> list = entry1.getValue();
                        List<QuotationSysVerModelVo> listNew = BeanUtil.copyToList(list,QuotationSysVerModelVo.class);
                        modelListMapNew.put(key,listNew);
                    }
                    quotationSystemVo.setModelList(modelListMapNew);
                }
            }
        }

        //处理智能工作台
        if(CollectionUtil.isNotEmpty(systemClassificationVoList)){
            List<IntelligentWorkbenchVo> intelligentWorkbenchVoList = new ArrayList<>();
            List<SystemClassificationVo> intelligentWorkbench = systemClassificationVoList.stream().filter(obj->obj.getSystemClassificationName().equals(intelligentWorkbenchName)).collect(Collectors.toList());
            SystemClassificationVo systemClassificationVo = intelligentWorkbench.get(0);
            List<QuotationSystemVo> systemVersionModelVoList = systemClassificationVo.getSysList();
            for(QuotationSystemVo sys:systemVersionModelVoList){
                Map<String, List<QuotationSysVerModelVo>> map = sys.getModelList();
                for(Map.Entry<String, List<QuotationSysVerModelVo>> entry:map.entrySet()){
                    List<QuotationSysVerModelVo> models = entry.getValue();
                    for(QuotationSysVerModelVo m:models){
                        IntelligentWorkbenchVo intelligentWorkbenchVo = new IntelligentWorkbenchVo();
                        intelligentWorkbenchVo.setClassificationName("【标配】" + systemClassificationVo.getSystemClassificationName());
                        intelligentWorkbenchVo.setVersion("标准版");
                        intelligentWorkbenchVo.setModelConfig("标配");
                        intelligentWorkbenchVo.setSystemName(m.getModelName());
                        intelligentWorkbenchVo.setUnitPrice(m.getModelPrice());
                        intelligentWorkbenchVo.setModelDesc(m.getModelDesc());
                        intelligentWorkbenchVo.setModelUnit(m.getModelUnit());
                        intelligentWorkbenchVoList.add(intelligentWorkbenchVo);
                    }
                }
            }
            compQuotationInfoVo.setIntelligentWorkbenchVoList(intelligentWorkbenchVoList);
            List<SystemClassificationVo> systemClassificationVoFilterZntList = systemClassificationVoList.stream().filter(obj->!obj.getSystemClassificationName().equals(intelligentWorkbenchName)).collect(Collectors.toList());
            compQuotationInfoVo.setSystemClassificationVoList(systemClassificationVoFilterZntList);
        }

        return compQuotationInfoVo;
    }
}
